<?php

class IndexModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showIndex($params) {
    	$HotProducts = ProductsManager::getInstance()->getAll("main_page=1", "RAND()", "0,3");
    	$request = fvRequest::getInstance();
    	$sort = $request->getRequestParameter("sort");
		$pp = $request->getRequestParameter("pp");
		if ($pp){
			fvSite::$fvSession->set("pp", $pp);
		}
		else{
			fvSite::$fvSession->set("pp", 10);
		}
    	switch ($sort){
			case 'title' : $sort = 'title'; $direction = 'ASC'; break;
			case 'price' : $sort = 'price'; $direction = 'ASC'; break;
			case 'date' : $sort = 'update_date'; $direction = 'DESC'; break;
			default : $sort = 'update_date'; $direction = 'DESC'; break;
		}
    	
    	$pager = new fvPager(ProductsManager::getInstance());
    	$fvTree = new fvTree(SectionsManager::getInstance()->getSectionTree());
    	$sections_options[0] = 'Усі категорії';
	    $sections_options += $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
        $this->__assign("pp", fvSite::$fvSession->get("pp"));
        $this->__assign("request", $request);
	    $this->__assign("PerPage", array("10" => "10","20" => "20","30" => "30","40" => "40","50" => "50",));
    	$this->__assign(array(
            'HotProducts' => $HotProducts,
    		'Products' => $pager->paginate(null, $sort.' '.$direction)
        ));
        return $this->__display('index_index.tpl'); 
    }
}

?>
